package com.ccrfid.rmc6.resource.device;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.device.AlerterEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.resource.device.pojo.FloorPos;
import com.ccrfid.rmc6.service.device.AlerterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("alerters")
@Produces(MediaType.APPLICATION_JSON)
public class AlerterResource {
    private static final Logger logger = LoggerFactory.getLogger(AlerterResource.class);
    @Autowired
    AlerterService alerterService;

    @GET
    public List<AlerterEntity> getAlerterList(@QueryParam("floorNumber") Integer floorNumber,
                                           @QueryParam("pos") String pos) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (pos != null) {
            if (pos.equalsIgnoreCase("Null")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NULL, pos));
            } else if (pos.equalsIgnoreCase("NotNull")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NOT_NULL, pos));
            }
        }
        List<AlerterEntity> list = alerterService.getAlerterList(criteria, Sort.by(new ArrayList<>()));
        return list;
    }

    @GET
    @Path("search")
    public Page<AlerterEntity> getAlerterInfoPage(
            @QueryParam("alerterName") String alerterName,
            @QueryParam("ip") String ip,
            @QueryParam("alerterType") Integer alerterType,
            @QueryParam("enabled") Integer enabled,
            @QueryParam("floorNumber") Integer floorNumber,
            @QueryParam("pos") String pos,
            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (alerterName != null) {
            criteria.add(new Criterion("alerterName", Criterion.Operator.LIKE, '%' + alerterName + '%'));
        }
        if (ip != null) {
            criteria.add(new Criterion("ip", Criterion.Operator.LIKE, '%' + ip + '%'));
        }
        if (alerterType != null) {
            criteria.add(new Criterion("alerterType", Criterion.Operator.EQ, alerterType));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (pos != null) {
            if (pos.equalsIgnoreCase("Null")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NULL, pos));
            } else if (pos.equalsIgnoreCase("NotNull")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NOT_NULL, pos));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }

        Page<AlerterEntity> page = alerterService.getAlerterInfoPage(criteria, pageRequest);
        return page;
    }

    @GET
    @Path("{id}")
    public AlerterEntity getAlerter(@PathParam("id") Long alerterId) {
        AlerterEntity alerterEntity = alerterService.getAlerterById(alerterId);
        return alerterEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public AlerterEntity updateAlerter(@PathParam("id") Long alerterId, AlerterEntity entity) throws AppException {
        AlerterEntity alerterEntity = alerterService.updateAlerter(alerterId, entity);
        return alerterEntity;
    }

    @PUT
    @Path("{id}/position")
    @Consumes(MediaType.APPLICATION_JSON)
    public AlerterEntity updatePosition(@PathParam("id") Long alerterId, FloorPos floorPos) throws AppException {
        return alerterService.updatePosition(alerterId, floorPos);
    }

    @DELETE
    @Path("{id}/position")
    public Boolean deletePosition(@PathParam("id") Long alerterId) throws AppException {
        return alerterService.deletePosition(alerterId);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public AlerterEntity createAlerter(AlerterEntity entity) throws AppException {
        AlerterEntity alerterEntity = alerterService.createAlerter(entity);
        return alerterEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteAlertersInfo(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return alerterService.deleteAlertersInfo(idList);
    }

    @PUT
    @Path("{id}/switch/{status}")
    public void switchControl(@PathParam("id") Long alerterId, @PathParam("status") Integer status){
        alerterService.switchControl(alerterId, status);
    }

}
